package com.chainyoung.component_tmperson.ui.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.BaseDialog;
import com.chainyoung.common.view.CustomEditor;
import com.chainyoung.common.view.CustomKeyBoard;
import com.chainyoung.common.view.kprogresshud.KProgressHUD;
import com.chainyoung.component_tmperson.R;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class WalletFinancingTurnOutDialog {


    private Context mContext;
    private BaseDialog mDialog;

    private TextView tvOwnNum;
    private TextView tvBuyAll;
    private EditText etNum;

    private String ownNum;

    private ConfirmTurnOutListener confirmListener;


    public WalletFinancingTurnOutDialog(Context context) {
        mContext = context;

        init();
    }

    public WalletFinancingTurnOutDialog(Context context, String ownNum) {
        mContext = context;
        this.ownNum = ownNum;
        init();
    }


    private void init() {
        int width = ConvertUtils.px2dp(mContext, ScreenUtils.getScreenWidth(mContext));
        int height = ConvertUtils.px2dp(mContext, ScreenUtils.getScreenHeight(mContext));
        BaseDialog.Builder mBuilder = new BaseDialog.Builder(mContext);
        mDialog = mBuilder.setViewId(R.layout.layout_confirm_turn_out)
                .setAnimation(R.style.Pop_aniamtion)
                .setGravity(Gravity.BOTTOM)
                .isCanceled(true)
                .isOnTouchCanceled(true)
                .setHeightdp(height / 2)
                .setWidthdp(width)
                .builder();
        tvOwnNum = mDialog.getView(R.id.tv_own_num);
        tvBuyAll = mDialog.getView(R.id.tv_buy_all);
        etNum = mDialog.getView(R.id.et_num);

        tvOwnNum.setText("当前可提取：" + StringUtil.subZeroAndDot(ownNum));


        mDialog.getView(R.id.tv_buy_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etNum.setText(ownNum);
                etNum.setSelection(etNum.getText().toString().length());
            }
        });
        mDialog.getView(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.close();
            }
        });

        mDialog.getView(R.id.tv_turn_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(StringUtil.viewGetText(etNum))) {
                    ToastUtil.showToast("请输入转出数量");
                    return;
                }
                if (confirmListener != null) {
                    confirmListener.onConfirmTurnOut(StringUtil.viewGetText(etNum));
                    mDialog.close();
                }
            }
        });

        etNum.addTextChangedListener(new TextWatcher() {
            boolean deleteLastChar;// 是否需要删除末尾

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    // 如果点后面有超过6位数值,则删掉最后一位
                    int length = s.length() - s.toString().lastIndexOf(".");
                    // 说明后面有8位数值
                    deleteLastChar = length >= 10;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    return;
                }

                if (deleteLastChar) {
                    // 设置新的截取的字符串
                    etNum.setText(s.toString().substring(0, s.toString().length() - 1));
                    // 光标强制到末尾
                    etNum.setSelection(etNum.getText().length());
                }

                // 以小数点开头，前面自动加上 "0"
                if (s.toString().startsWith(".")) {
                    etNum.setText("0" + s);
                    etNum.setSelection(etNum.getText().length());
                }
                if (s.toString().startsWith("0") && (s.toString().length() > 1) && !s.toString().startsWith("0.")) {
                    etNum.setText(s.toString().substring(1));
                    etNum.setSelection(etNum.getText().length());
                }


                if (new BigDecimal(etNum.getText().toString().trim()).compareTo(new BigDecimal(ownNum)) > 0) {
                    etNum.setText(ownNum);
                    etNum.setSelection(etNum.getText().toString().length());
                }
            }
        });


    }


    public void show() {
        mDialog.show();
    }

    public ConfirmTurnOutListener getConfirmListener() {
        return confirmListener;
    }

    public void setConfirmListener(ConfirmTurnOutListener confirmListener) {
        this.confirmListener = confirmListener;
    }


    public interface ConfirmTurnOutListener {
        void onConfirmTurnOut(String buyNum);
    }


}
